<script>
  import WeAppRedux from '@/pages/subPage/liveVideoArea/nim/redux/index.js'
  import createStore from '@/pages/subPage/liveVideoArea/nim/redux/createStore.js'
  import reducer from '@/pages/subPage/liveVideoArea/nim/store/reducer.js'
  import nim from '@/utils/nim.js'
  import { mapState } from 'vuex'
  const {Provider} = WeAppRedux
  const store = createStore(reducer) // redux store
   import storage from '@/utils/storage'
   let app = getApp()
export default {
  created () {
    let that = this
    // 调用API从本地缓存中获取数据
    /*
     * 平台 api 差异的处理方式:  api 方法统一挂载到 mpvue 名称空间, 平台判断通过 mpvuePlatform 特征字符串
     * 微信：mpvue === wx, mpvuePlatform === 'wx'
     * 头条：mpvue === tt, mpvuePlatform === 'tt'
     * 百度：mpvue === swan, mpvuePlatform === 'swan'
     * 支付宝(蚂蚁)：mpvue === my, mpvuePlatform === 'my'
     */

    let logs
    if (mpvuePlatform === 'my') {
      logs = mpvue.getStorageSync({key: 'logs'}).data || []
      logs.unshift(Date.now())
      mpvue.setStorageSync({
        key: 'logs',
        data: logs
      })
    } else {
      logs = mpvue.getStorageSync('logs') || []
      logs.unshift(Date.now())
      mpvue.setStorageSync('logs', logs)
    }
    //清除两天前的失败消息
    let localMsgArr = storage.getLocalMsg();
    let now = new Date().getTime();
    localMsgArr = localMsgArr.filter(item =>{
      return now - item.time < 2 * 24 * 60 * 60 * 1000
    }).map(item =>{
      item.status=nim.msgStatus_fail
      return item
    })
    storage.setLocalMsg(localMsgArr)
    // 清空2天前保存的消息
    let arr = this.$storage.getMsg() || [] // 本地缓存数据
    arr = arr.filter(item =>{
      return now - item.endTime * 1000 < 1 * 24 * 60 * 60 * 1000
    })
    storage.saveMsg(arr)
  },
  onShow (res) {
    let that = this
    // 重新进入后台
    if (res.query) {
      let source = res.query.source ? res.query.source : '0'
      console.log(source, 'source')
      storage.setSouruce(source)
    }
    nim.reconnection()
    wx.onAppHide((res) =>{//mpvue无此生命周期，使用wx
      console.log('再次监听onhide')
      let pages = getCurrentPages()
      console.log()
      let currentPage = pages[pages.length - 1] || {route: ''}
      let currentPage2 = pages[pages.length - 2] || {route: ''}
      if (currentPage.route.includes('videoCall') === true) {
        setTimeout(() => {
          mpvue.navigateBack(1)
        }, 1000)
      } else if (currentPage2.route.includes('videoCall') === true) {
        mpvue.navigateBack({
          delta: 2
        })
      }
      // 防止视频中先注销了  一些方法报错导致患者不能结束视频
      setTimeout(() => {
        nim.destroy()
      }, 1000)
    })
    // 每次从后台前往前台都检测小程序是否需要更新，不是最新版本的话要请求更新
    const updateManager = mpvue.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
    })
    updateManager.onUpdateReady(function () {
      mpvue.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success(res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
  },
  computed: {
    ...mapState([
      'chatTo'
    ])
  },
  log () {
    console.log(`log at:${Date.now()}`)
  }
}
</script>

<style lang="less">
page {
  width: 100%;
  height: 100%!important;
  background: #fff;
  -webkit-overflow-scrolling: touch;
  box-sizing: border-box!important;
  overflow: auto!important;
  /* font-family:PingFang-SC-Heavy; */
}
.subPage {
  width: 100%;
  height: 100%;
  box-sizing: border-box!important;
  color: #333333;
}
.allow {
  width: 516rpx;
  height: 472rpx;
  position: absolute;
  left: 50%;
  top: 258rpx;
  margin-left: -258rpx;
}
.nowrap {
  white-space:nowrap;
  text-overflow:ellipsis;
  overflow:hidden;
}
.fw500 {
  font-weight: 500;
}
.noData {
  text-align: center;
  margin-top: 50rpx;
}
.fullscreen {
  width: 100%;
  height: 100%;
  overflow: hidden!important;
}
.background-wrapper {
  width: 100%;
  height: 100%;
  background-color: #fff;
}
.cancel {
  background: #F8F8FA;
}
.bottomDiv {
  position: relative;
  height: 140rpx;
  .recode {
    position: absolute;
    width: 100%;
    text-align: center;
    height: 50rpx;
    line-height: 50rpx;
    margin-top: 35rpx;
    color: #fff;
    font-size: 18px;
  }
  img {
    height: 100%;
    width: 100%;
  }
}
// 聊天图片加载 loading 图标动画
@keyframes myfirst {
  from {transform: rotate(0deg);}
  to {transform: rotate(360deg);}
}
.animationIcon {
  font-size: 20px;
  transform-origin:50% 50%;
  animation-name: myfirst;
  animation-duration: 1s;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
}
// 线上问诊 查询下拉框
.doctorListMenu {
  .van-dropdown-menu {
    height: 90rpx;
    background-color: #F9F9f9;
    .van-dropdown-menu__title {
      color: #999999;
      line-height: 90rpx;
    }
    .activeTitle {
      font-weight: 600;
      color: #1989fa;
    }
    .van-dropdown-menu__title--active {
      color: #1989fa;
    }
    .van-dropdown-item__icon {
      display: none;
    }
    .van-dropdown-menu__item:nth-child(2)
     {
      color: #999999!important;
      .van-dropdown-menu__title:after {
        content:'';
        display:block;
        width: 16rpx;
        height: 22rpx;
        background: url("https://ks3-cn-beijing.ksyun.com/wxapp/wdrm/image/patient/unchooseTangle.png") no-repeat;
        background-size:cover;
        position: absolute;
        transform: rotate(0deg);
        left: 100%;
        border: none;
        margin-top: -10rpx!important;
      }
      .van-dropdown-menu__title--down:after {
        margin-top: -10rpx!important;
        transform: rotate(180deg);
      }
    }
    .van-dropdown-menu__item:nth-child(2) .van-dropdown-menu__title--active{
      color: #999999;
    }
    .van-popup {
      background-color: rgba(0, 0, 0, 0);
    }
    .van-cell {
      height: 100rpx;
      line-height: 60rpx;
    }
    .van-dropdown-item__option:last-child {
      .van-cell {
        border-radius:0px 0px 20rpx 20rpx;
      }
    }
    .van-dropdown-item__title {
      padding-left: 42rpx;
      font-size: 28rpx;
    }
    .van-dropdown-item__option--active {
      .van-cell {
        background-color: #EBF0FF;
      }
    }
  }
}
.specVanItem {
  .van-dropdown-menu {
  .van-dropdown-menu__item:nth-child(2){
      color: #999999!important;
      .van-dropdown-menu__title:after {
        content:'';
        display:block;
        width: 16rpx;
        height: 22rpx;
        background: url("https://ks3-cn-beijing.ksyun.com/wxapp/wdrm/image/patient/chooseTangle.png") no-repeat;
        background-size:cover;
        position: absolute;
        transform: rotate(0deg);
        left: 100%;
        border: none;
        margin-top: -10rpx!important;
      }
      .van-dropdown-menu__title--down:after {
        margin-top: -10rpx!important;
        transform: rotate(180deg);
      }
    }
  }
}
.noDataImg {
  width: 100%;
  height: 100%;
  position: relative;
  background: #fff;
  img {
    width: 620rpx;
    height: 470rpx;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -310rpx;
    margin-top: -235rpx;
  }
  // .prescript{
  //   top: 37%;
  // }
}
.textColor {
  color: #258FF8;
}
.ipxPage {
  padding-bottom: 30rpx;
}
button{
  padding-left: 10rpx;
  padding-right: 10rpx;
}
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;}
body,input,textarea,select,button,table{border-collapse:collapse;border-spacing:0;}
img,fieldset{border:0 none;}
ul,ol,li{list-style:none;}
em,address{font-style:normal;}
table{border-collapse:collapse;}
em,i{font-style:normal;}
strong,b{font-weight:normal;}
img{border:none;}
.clearfix:after {
  display: block;
  clear: both;
  visibility: hidden;
  height: 0;
  content: " ";
  font-size: 0;
}
.clearfix {
  zoom: 1;
}
.fl {
  float: left;
}
.fr {
  float: right;
}
/* this rule will be remove */
* {
  transition: width 2s;
  -moz-transition: width 2s;
  -webkit-transition: width 2s;
  -o-transition: width 2s;
}
.i-swipeout-demo-item {
  height: 160rpx!important;
  overflow: hidden;
}
.cell{
    border-top:1px solid #e9e9e9;
    input{
      color: #666;
    }
}
.botCell{
    border-bottom: 1px solid #e9e9e9
}
.footer{
  .i-cell{
    padding: 0!important;
  }
  .i-radio{
    height: 24rpx;
    line-height: 24rpx;
    .i-cell{
      padding: 0!important;
    }
  }
}
.i-radio{
    // height: 24rpx;
    // line-height: 24rpx;
    .i-cell{
      padding: 0!important;
    }
  }
.spin{
  background-color: #fff!important;
  border: 1rpx solid #f3f3f3;
  border-radius: 4px;
}
.i-modal-grid-item-last{
  background-color: #4A64EA!important;
  color: #fff!important;
}
.i-modal-grid-item,.i-modal-grid-item-last {
  background-color: #4A64EA!important;
  color: #fff!important;
}
.nowTab,
.itemTabs2 {
  font-weight: 700!important;
}
</style>
